package com.plugins.usereflect;

import java.io.File;
import java.util.Hashtable;
import java.util.LinkedHashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.zxy.adminlog.UsAdmin_Log;
import com.zxy.business.Ope_DB_Cent;
import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;

//导入单表信息
public class A01_INTABL_IN implements com.zxy.common.ParamName_InterFace
{
	public StringBuilder	strResult		= new StringBuilder();
	public String			user_main_id			= null;
	public String			session_id		= null;
	public String			param_name		= "";
	//文件名
	public String			param_value1	= null;
	//表名
	public String			param_value2	= null;
	//数据源 ID
	public String			param_value3	= null;
	public String			param_value4	= null;
	public String			param_value5	= null;
	public String			param_value6	= null;
	public String			param_value7	= null;
	public String			param_value8	= null;
	public String			param_value9	= null;
	public String			param_value10	= null;
	public String 			temStrTFileName = "";
	//表关键字段,唯一性
	public Hashtable<String,String[]> htTb_Key = new Hashtable<String,String[]>();
	//主从关联表
	public JSONObject jsoTB_LINK = new JSONObject(new LinkedHashMap());
	
	public void init_start()
	{	
		if(!java.util.Objects.equals(this.param_value1,null)
				&& !this.param_value1.equals("")){
			if(com.zxy.common.Com_Para.UpLoad_File.equals("")){
				temStrTFileName = Com_Fun.GetAbsPath() + this.param_value1;
			}else{
				temStrTFileName = com.zxy.common.Com_Para.UpLoad_File
						+ Com_Para.zxyPath + this.param_value1;
			}
		}
		else
			temStrTFileName = this.param_value10;
		File fiFile = new File(temStrTFileName);
		if(!fiFile.isFile()){
			this.strResult.delete(0,this.strResult.length());
			this.strResult.append(Com_Fun.get_ope_result(param_name,"0","异常:附件文件不存在"));
		}else
		{
			// 线程异步调用
			Runnable Thread_A01_INTABL_IN = new Runnable(){
				//初始化各表之间关联关系
				public void Init_TB() throws JSONException{
					JSONArray jsoary = new JSONArray();
					//关联信息定义
					JSONObject jsoTb_PID = new JSONObject(new LinkedHashMap());
					
					/*单表中数据键值唯一*/
					htTb_Key.put("t_db_config",new String[] {"MAIN_ID","DB_URL"});
					htTb_Key.put("t_param_value",new String[] {"PARAM_KEY"});
					htTb_Key.put("t_sub_sys",new String[] {"SUB_CODE"});
					htTb_Key.put("t_sub_user",new String[] {"SUB_USERCODE"});
					htTb_Key.put("menu_group",new String[] {"GROUP_EN_NAME"});
					htTb_Key.put("menu_info",new String[] {"MENU_URL"});
					htTb_Key.put("menu_content",new String[] {"MAIN_ID"});
					htTb_Key.put("t_user_info",new String[] {"LOGIN_ID"});
					htTb_Key.put("t_role",new String[] {"ROLE_NAME"});
					
					htTb_Key.put("t_single_table",new String[] {"TABLE_EN_NAME"});
					htTb_Key.put("t_single_expand",new String[] {"TABLE_ID"});
					
					htTb_Key.put("t_table_column",new String[] {"TABLE_ID","COLUMN_EN_NAME"});
					htTb_Key.put("t_column_expand",new String[] {"COLUMN_ID"});
//					//从表外键
//					jsoTb_PID.put("P_ID","SUB_ID");
//					//主表名称
//					jsoTb_PID.put("P_TABLE","t_sub_sys");
//					//主表主键
//					jsoTb_PID.put("P_KEY","MAIN_ID");
//					//主表唯一业务值
//					jsoTb_PID.put("P_BIZCODE","SUB_CODE");					
					jsoary = new JSONArray();
					jsoary.put(new JSONObject("{\"P_ID\":\"SUB_ID\",\"P_TABLE\":\"t_sub_sys\",\"P_KEY\":\"MAIN_ID\",\"P_BIZCODE\":\"SUB_CODE\"}"));
					jsoTB_LINK.put("t_sub_user",jsoary);
					
					htTb_Key.put("t_page_info",new String[] {"PAGE_CODE"});
					htTb_Key.put("t_proc_name",new String[] {"INF_EN_NAME"});
					htTb_Key.put("t_page_item",new String[] {"ITEM_NAME"});
					jsoary = new JSONArray();
					jsoary.put(new JSONObject("{\"P_ID\":\"PAGE_ID\",\"P_TABLE\":\"t_page_info\",\"P_KEY\":\"MAIN_ID\",\"P_BIZCODE\":\"PAGE_CODE\"}"));	
					jsoary.put(new JSONObject("{\"P_ID\":\"PROC_ID\",\"P_TABLE\":\"t_proc_name\",\"P_KEY\":\"MAIN_ID\",\"P_BIZCODE\":\"INF_EN_NAME\"}"));
					jsoTB_LINK.put("t_page_item",jsoary);
					
					jsoary = new JSONArray();
					jsoary.put(new JSONObject("{\"P_ID\":\"GROUP_ID\",\"P_TABLE\":\"menu_group\",\"P_KEY\":\"MAIN_ID\",\"P_BIZCODE\":\"GROUP_EN_NAME\"}"));
					jsoTB_LINK.put("menu_info",jsoary);

					jsoary = new JSONArray();
					jsoary.put(new JSONObject("{\"P_ID\":\"MENU_ID\",\"P_TABLE\":\"menu_info\",\"P_KEY\":\"MAIN_ID\",\"P_BIZCODE\":\"MENU_ID\"}"));
					jsoTB_LINK.put("menu_content",jsoary);
					
					jsoary = new JSONArray();
					jsoary.put(new JSONObject("{\"P_ID\":\"TABLE_ID\",\"P_TABLE\":\"t_single_table\",\"P_KEY\":\"MAIN_ID\",\"P_BIZCODE\":\"TABLE_EN_NAME\"}"));
					jsoTB_LINK.put("t_single_expand",jsoary);
					
					jsoary = new JSONArray();
					jsoary.put(new JSONObject("{\"P_ID\":\"TABLE_ID\",\"P_TABLE\":\"t_single_table\",\"P_KEY\":\"MAIN_ID\",\"P_BIZCODE\":\"TABLE_EN_NAME\"}"));
					jsoTB_LINK.put("t_table_column",jsoary);
					
					jsoary = new JSONArray();
					jsoary.put(new JSONObject("{\"P_ID\":\"COLUMN_ID\",\"P_TABLE\":\"t_table_column\",\"P_KEY\":\"MAIN_ID\",\"P_BIZCODE\":\"MAIN_ID\"}"));
					jsoTB_LINK.put("t_column_expand",jsoary);
					
					//业务表json格式导入案例
					htTb_Key.put("h_device_info",new String[] {"STATION_CODE","DEVICE_CODE"});
				}
				
				@Override
				public void run(){
					try
					{
						Init_TB();
						
						UsAdmin_Log uL = new UsAdmin_Log(Com_Para.ApplicationPath,new StringBuilder(""));
						StringBuilder sbo = uL.ReadFile(temStrTFileName);
						JSONObject jsonObject = new JSONObject(sbo.toString());
						
						//特殊的表处理含主从表及关联表
						if(java.util.Objects.equals(param_value2,"特殊的表处理")){
						}
						else if(java.util.Objects.equals(param_value2.toLowerCase(),"t_page_item")){
							JSON_in_DB(jsonObject,"t_page_info");
							JSON_in_DB(jsonObject,"t_proc_name");
							//单一表处理
							JSON_in_DB(jsonObject,param_value2);
						}
						else if(java.util.Objects.equals(param_value2.toLowerCase(),"t_sub_user")){
							JSON_in_DB(jsonObject,"t_sub_sys");
							//单一表处理
							JSON_in_DB(jsonObject,param_value2);
						}
						else if(java.util.Objects.equals(param_value2.toLowerCase(),"menu_info")){
							JSON_in_DB(jsonObject,"menu_group");
							//单一表处理
							JSON_in_DB(jsonObject,param_value2);
						}
						else if(java.util.Objects.equals(param_value2.toLowerCase(),"menu_content")){
							JSON_in_DB(jsonObject,"menu_info");
							//单一表处理
							JSON_in_DB(jsonObject,param_value2);
						}
						else if(java.util.Objects.equals(param_value2.toLowerCase(),"t_single_expand")){
							//单一表处理
							JSON_in_DB(jsonObject,"t_single_table");
							JSON_in_DB(jsonObject,"t_single_expand");
							JSON_in_DB(jsonObject,"t_table_column");
							JSON_in_DB(jsonObject,"t_column_expand");
						}
						else{
							//单一表处理
							JSON_in_DB(jsonObject,param_value2);
						}
					}
					catch(Exception e)
					{
						Com_Fun.debug_in(e,this.toString());
					}
				}
				
				//JSON数据格式入库
				public void JSON_in_DB(JSONObject jsonObject,String table_name) throws JSONException{
					Ope_DB_Cent opc = new Ope_DB_Cent();
					JSONArray jsary = jsonObject.getJSONArray(table_name);
					for(int i = 0;i < jsary.length();i++){
						JSONObject jso_obj = jsary.getJSONObject(i);
						if(!htTb_Key.containsKey(table_name))
							opc.Ins_TABLE_Data_JSON(jso_obj,table_name,param_value3,new String[0],new String[0]);
						else{
							String strSql = "select * from "+table_name+" where 1=1 ";
							String[] KeysL = (String[])(htTb_Key.get(table_name));
							String[] temParameters = new String[KeysL.length];
							String[] temParamTypes = new String[KeysL.length];
							for(int n = 0; n < temParameters.length; n ++){
								temParameters[n] = jso_obj.getString(KeysL[n]);
								temParamTypes[n] = "STRING";
								strSql += " and "+KeysL[n]+"=?";
							}
							if(!opc.Had_Data_By_Sql(strSql,temParameters,temParamTypes,param_value3)){
								String[] PID_key = new String[0];
								String[] PID_value = new String[0];
								if(jsoTB_LINK.has(table_name)){
									JSONArray temJary = jsoTB_LINK.getJSONArray(table_name);
									PID_key = new String[temJary.length()];
									PID_value = new String[temJary.length()];
									for(int ik = 0; ik < temJary.length(); ik ++){
										PID_key[ik] = temJary.getJSONObject(ik).getString("P_ID");
										
										//从表外键value
										String P_ID = jso_obj.getString(PID_key[ik]);
										JSONArray jaryP_TABLE = jsonObject.getJSONArray(temJary.getJSONObject(ik).getString("P_TABLE"));
										for(int nt = 0; nt <jaryP_TABLE.length(); nt ++){
											String P_MAIN_ID = jaryP_TABLE.getJSONObject(nt).getString(temJary.getJSONObject(ik).getString("P_KEY"));
											String P_SUB_CODE = jaryP_TABLE.getJSONObject(nt).getString(temJary.getJSONObject(ik).getString("P_BIZCODE"));
											if(java.util.Objects.equals(P_ID,P_MAIN_ID)){
												PID_value[ik] = opc.get_key_by_foreignkey(temJary.getJSONObject(ik).getString("P_TABLE"),
														temJary.getJSONObject(ik).getString("P_KEY"),temJary.getJSONObject(ik).getString("P_BIZCODE"),P_SUB_CODE);
												break;
											}
										}
												
									}
								}
								opc.Ins_TABLE_Data_JSON(jso_obj,table_name,param_value3,PID_key,PID_value);
							}									
						}
					}
				}
			};
			
			// 异步调用
			Thread authThread = new Thread(Thread_A01_INTABL_IN);
			authThread.start();
			this.strResult.delete(0,this.strResult.length());
			this.strResult.append(Com_Fun.get_ope_result(param_name,"1",this.param_value2+"表数据导入中，请等待"));
		}
	}

	/**
	 * @return strResult
	 */
	public StringBuilder getStrResult()
	{
		return strResult;
	}

	/**
	 * @param strResult 要设置的 strResult
	 */
	public void setStrResult(StringBuilder strResult)
	{
		this.strResult = strResult;
	}

	/**
	 * @return user_main_id
	 */
	public String getUser_main_id()
	{
		return user_main_id;
	}

	/**
	 * @param user_main_id 要设置的 user_main_id
	 */
	public void setUser_main_id(String user_main_id)
	{
		this.user_main_id = user_main_id;
	}

	/**
	 * @return session_id
	 */
	public String getSession_id()
	{
		return session_id;
	}

	/**
	 * @param session_id 要设置的 session_id
	 */
	public void setSession_id(String session_id)
	{
		this.session_id = session_id;
	}

	/**
	 * @return param_name
	 */
	public String getParam_name()
	{
		return param_name;
	}

	/**
	 * @param param_name 要设置的 param_name
	 */
	public void setParam_name(String param_name)
	{
		this.param_name = param_name;
	}

	/**
	 * @return param_value1
	 */
	public String getParam_value1()
	{
		return param_value1;
	}

	/**
	 * @param param_value1 要设置的 param_value1
	 */
	public void setParam_value1(String param_value1)
	{
		this.param_value1 = param_value1;
	}

	/**
	 * @return param_value2
	 */
	public String getParam_value2()
	{
		return param_value2;
	}

	/**
	 * @param param_value2 要设置的 param_value2
	 */
	public void setParam_value2(String param_value2)
	{
		this.param_value2 = param_value2;
	}

	/**
	 * @return param_value3
	 */
	public String getParam_value3()
	{
		return param_value3;
	}

	/**
	 * @param param_value3 要设置的 param_value3
	 */
	public void setParam_value3(String param_value3)
	{
		this.param_value3 = param_value3;
	}

	/**
	 * @return param_value4
	 */
	public String getParam_value4()
	{
		return param_value4;
	}

	/**
	 * @param param_value4 要设置的 param_value4
	 */
	public void setParam_value4(String param_value4)
	{
		this.param_value4 = param_value4;
	}

	/**
	 * @return param_value5
	 */
	public String getParam_value5()
	{
		return param_value5;
	}

	/**
	 * @param param_value5 要设置的 param_value5
	 */
	public void setParam_value5(String param_value5)
	{
		this.param_value5 = param_value5;
	}

	/**
	 * @return param_value6
	 */
	public String getParam_value6()
	{
		return param_value6;
	}

	/**
	 * @param param_value6 要设置的 param_value6
	 */
	public void setParam_value6(String param_value6)
	{
		this.param_value6 = param_value6;
	}

	/**
	 * @return param_value7
	 */
	public String getParam_value7()
	{
		return param_value7;
	}

	/**
	 * @param param_value7 要设置的 param_value7
	 */
	public void setParam_value7(String param_value7)
	{
		this.param_value7 = param_value7;
	}

	/**
	 * @return param_value8
	 */
	public String getParam_value8()
	{
		return param_value8;
	}

	/**
	 * @param param_value8 要设置的 param_value8
	 */
	public void setParam_value8(String param_value8)
	{
		this.param_value8 = param_value8;
	}

	/**
	 * @return param_value9
	 */
	public String getParam_value9()
	{
		return param_value9;
	}

	/**
	 * @param param_value9 要设置的 param_value9
	 */
	public void setParam_value9(String param_value9)
	{
		this.param_value9 = param_value9;
	}

	/**
	 * @return param_value10
	 */
	public String getParam_value10()
	{
		return param_value10;
	}

	/**
	 * @param param_value10 要设置的 param_value10
	 */
	public void setParam_value10(String param_value10)
	{
		this.param_value10 = param_value10;
	}
}
